#ifndef SERVER_SIMULATOR_H
#define SERVER_SIMULATOR_H

#include <cstddef>
#include <cstdint>

#ifdef __cplusplus
extern "C" {
#endif

// C接口声明
void* server_create(const char* manufacturer, const char* sn, int disk_count, const std::size_t* disk_sizes);
void  server_destroy(void* server);
int   server_power_on(void* server);
int   server_power_off(void* server);
int   server_is_powered_on(void* server);
int   server_set_fault(void* server, int fault_code);
int   server_clear_fault(void* server);
int   server_get_fault_code(void* server);
int   server_get_disk_count(void* server);
std::size_t server_get_disk_size(void* server, int disk_index);
int   server_get_manufacturer(void* server, char* manufacturer, std::size_t len);
int   server_get_sn(void* server, char* sn, std::size_t len);

// 新增服务器信息结构体
struct ServerInfo {
    int powered_on;           // 服务器是否上电
    int fault_code;           // 故障码
    int disk_count;           // 挂载磁盘数量
    std::size_t disk_sizes[32]; // 每个磁盘容量，最多32个盘
    char manufacturer[64];    // 制造商
    char sn[64];              // 序列号
};

// 新增C接口：查询服务器信息
int server_get_info(void* server, struct ServerInfo* info);

// 新增：通过SN读写磁盘的接口
int server_read_disk_by_sn(const char* server_sn, const char* disk_sn, std::size_t offset, void* buf, std::size_t len);
int server_write_disk_by_sn(const char* server_sn, const char* disk_sn, std::size_t offset, const void* buf, std::size_t len);
int server_get_disk_sn(void* server, int disk_index, char* sn, std::size_t sn_len);

#ifdef __cplusplus
}
#endif

// C++服务器模拟器类
#include <vector>
#include <string>
#include <memory>

class ServerSimulator {
public:
    ServerSimulator(const std::string& manufacturer, const std::string& sn, int disk_count, const std::vector<std::size_t>& disk_sizes);
    ~ServerSimulator();
    int power_on();
    int power_off();
    bool is_powered_on() const;
    int set_fault(int fault_code);
    int clear_fault();
    int get_fault_code() const;
    int get_disk_count() const;
    std::size_t get_disk_size(int disk_index) const;
    std::string get_manufacturer() const;
    std::string get_sn() const;

    // 新增：声明友元函数
    friend int server_read_disk_by_sn(const char* server_sn, const char* disk_sn, std::size_t offset, void* buf, std::size_t len);
    friend int server_write_disk_by_sn(const char* server_sn, const char* disk_sn, std::size_t offset, const void* buf, std::size_t len);

private:
    struct DiskHandle;
    std::vector<DiskHandle*> disks_;
    std::string manufacturer_;
    std::string sn_;
    bool powered_on_;
    int fault_code_;
    void* disk_lib_handle_;
};

#endif // SERVER_SIMULATOR_H 